package com.dyh.quickdemozwei.hxfz;

import org.junit.Test;

import java.util.*;
  
public class Main {


    @Test
    public void test(){

        TreeMap<Integer, Set<String>> map = new TreeMap<>();
        map.put(1, new HashSet<>(Arrays.asList("颜色:红色", "颜色:黄色","颜色:蓝色")));
        map.put(3, new HashSet<>(Arrays.asList("重量:1.3", "重量:1.2")));


        TreeMap<Integer,Set<String>> lvResMap = new TreeMap<>();

        Integer fk = map.firstEntry().getKey();
        int lv = 0;
        for (String s : map.firstEntry().getValue()) {

            lvResMap.computeIfAbsent(lv,k->new HashSet<>())
                    .add(s);
//            map.ceilingEntry()
        }


        for (Map.Entry<Integer, Set<String>> entry : map.tailMap(fk+1).entrySet()) {
            lv++;
            for (String s : lvResMap.ceilingEntry(lv - 1).getValue()) {
                for (String s1 : entry.getValue()) {

                    lvResMap.computeIfAbsent(lv,k->new HashSet<>())
                            .add(  String.format("%s,%s",s,s1));
                }
            }

        }




        System.out.println();
    }

    public static void main(String[] args) {  
        TreeMap<Integer, Set<String>> map = new TreeMap<>();  
        map.put(1, new HashSet<>(Arrays.asList("颜色:红色", "颜色:黄色")));  
        map.put(3, new HashSet<>(Arrays.asList("重量:1.3", "重量:1.2")));
        map.put(4, new HashSet<>(Arrays.asList("长度:1", "长度:2")));


        TreeMap<Integer,Set<String>> lvResMap = new TreeMap<>();

        Integer fk = map.firstEntry().getKey();
        int lv = 0;
        for (String s : map.firstEntry().getValue()) {

            lvResMap.computeIfAbsent(lv,k->new HashSet<>())
                    .add(s);
//            map.ceilingEntry()
        }


        for (Map.Entry<Integer, Set<String>> entry : map.tailMap(fk+1).entrySet()) {
            lv++;
            for (String s : lvResMap.ceilingEntry(lv - 1).getValue()) {
                for (String s1 : entry.getValue()) {

                    lvResMap.computeIfAbsent(lv,k->new HashSet<>())
                            .add(  String.format("%s,%s",s,s1));
                }
            }

        }

        System.out.println();

        //包含k 的之后的
//        map.tailMap(2).forEach((k,v)-> System.out.println(k+":"+v));

//        generateCombinations(map);
    }  
  
    public static void generateCombinations(TreeMap<Integer, Set<String>> map) {  
        // 用于存储已经生成过的组合的Set  
        Set<String> generatedCombinations = new HashSet<>();  
  
        // 遍历TreeMap的每一个规格值  
        for (Map.Entry<Integer, Set<String>> entry1 : map.entrySet()) {  
            Set<String> specs1 = entry1.getValue();  
            for (String spec1 : specs1) {  
                // 获取规格名称和值  
                String[] parts1 = spec1.split(":");  
                String name1 = parts1[0];  
                String value1 = parts1[1];  
  
                // 对于其它所有的规格值，生成排列组合  
                for (Map.Entry<Integer, Set<String>> entry2 : map.entrySet()) {  
                    if (entry1.getKey() != entry2.getKey()) {  
                        Set<String> specs2 = entry2.getValue();  
                        for (String spec2 : specs2) {  
                            // 获取规格名称和值  
                            String[] parts2 = spec2.split(":");  
                            String name2 = parts2[0];  
                            String value2 = parts2[1];  
  
                            // 生成排列组合结果  
                            String combination = name1 + "=" + value1 + ", " + name2 + "=" + value2;  
                            if (!generatedCombinations.contains(combination)) {  
                                System.out.println(combination);  
                                generatedCombinations.add(combination);  
                            }  
                        }  
                    }  
                }  
            }  
        }  
    }  
}